<?php

if(define(__FILE__, true)) {
	
	class Rebel_Std_Env {

		
		const 	KEY_MATCH_EXACT = 1,
				KEY_MATCH_NOCASE = 2,
				PROTOCOL_SERVER = 1,
				PROTOCOL_SESSION = 2,
				PROTOCOL_GET= 3,
				PROTOCOL_POST = 4,
				PROTOCOL_FILES = 5,
				PROTOCOL_COOKIE = 6,
				PROTOCOL_REQUEST = 7;
				
		
		public static function get($uri) {
			
			$t = self::parseURI($uri);
			
			$r = new ReflectionClass(__CLASS__);
			
			$method = sprintf('get%s',ucfirst(array_shift($t)));
			
			if($r->hasMethod($method)) {

				$m = new ReflectionMethod(__CLASS__,$method);
				return $m->invoke(__CLASS__,$t);

			} else {
				
				throw new Exception(__CLASS__ . " NULL POINTER EXCEPTION");
			}
			
		}//:-
		
		private static function parseURI ($URI) {
			
			$uri = str_replace('://','.',$URI);
			$uri = strtolower(str_replace('/','.',$uri));
			$t = explode('.',$uri);
			return $t;
		}//:-
		
		public static function getSession($path) {
			$path = is_array($path) ? $path : self::parseURI($path);
			$path[0] == 'session' ? array_shift($path) : null;
			return self::arrayLookup($_SESSION,$path,self::KEY_MATCH_NOCASE);
		}//:-
		
		public static function getRequest($path) {
			$path = is_array($path) ? $path : self::parseURI($path);
			$path[0] == 'request' ? array_shift($path) : null;
			return self::arrayLookup($_REQUEST,$path,self::KEY_MATCH_NOCASE);
		}//:-
		
		public static function getPost($path) {
			$path = is_array($path) ? $path : self::parseURI($path);
			$path[0] == 'post' ? array_shift($path) : null;
			return self::arrayLookup($_POST,$path,self::KEY_MATCH_NOCASE);
		}//:-
		
		public static function getGet($path) {
			$path = is_array($path) ? $path : self::parseURI($path);
			$path[0] == 'get' ? array_shift($path) : null;
			return self::arrayLookup($_GET,$path,self::KEY_MATCH_NOCASE);
		}//:-
		
		public static function getServer($path) {
			$path = is_array($path) ? $path : self::parseURI($path);
			$path[0] == 'server' ? array_shift($path) : null;
			return self::arrayLookup($_SERVER,$path,self::KEY_MATCH_NOCASE);
		}//:-
		
		public static function getFiles($path) {
			$path = is_array($path) ? $path : self::parseURI($path);
			$path[0] == 'files' ? array_shift($path) : null;
			return self::arrayLookup($_FILES,$path,self::KEY_MATCH_NOCASE);
		}//:-
		
		public static function getCookie($path) {
			$path = is_array($path) ? $path : self::parseURI($path);
			$path[0] == 'cookie' ? array_shift($path) : null;
			return self::arrayLookup($_COOKIE,$path,self::KEY_MATCH_NOCASE);
		}//:-
		
		private static function arrayLookup($array,$path,$keymatch=self::KEY_MATCH_EXACT) {
			
			$t = $array;
			$iterations = count($path);
			for($i = 0; $i < $iterations; $i++) {
				
				$k = array_shift($path);
				
				if($keymatch == self::KEY_MATCH_EXACT) {
					
					if(isset($t[$k])) $t = $t[$k]; else throw new Exception('NULL POINTER EXCEPTION');
					
				} else {
					//AS IS
					if(isset($t[$k])) {
						$t = $t[$k];
					} elseif (isset($t[strtoupper($k)])) {
						//UPPER
						$t = $t[strtoupper($k)];
					} elseif (isset($t[strtolower($k)])) {
						//LOWER
						$t = $t[strtolower($k)];
					} else {
						throw new Exception('NULL POINTER EXCEPTION');
					}
				}

			}

			return $t;	

		}//:-
		
	}///:-

}

?>